package com.chapter04;

import com.bdqn.util.JDBCUtils;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class Test01_InertBlob {
    public static void main(String[] args) throws SQLException, ClassNotFoundException, FileNotFoundException {
        // todo 获取连接对象
        Connection connection = JDBCUtils.getConnection();

        // todo 获取PreparedStatement对象
        String sql = "insert into customers(name,email,birth,photo)values(?,?,?,?)";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        // 给占位符赋值
        preparedStatement.setObject(1, "孙老师");
        preparedStatement.setObject(2, "1186655448@qq.com");
        preparedStatement.setObject(3, "2001-11-12");
//        preparedStatement.setObject(4,"");

        // 比如存图片
        InputStream inputStream = new FileInputStream("file/aa.jpg");
        preparedStatement.setBlob(4, inputStream);

        int i = preparedStatement.executeUpdate();
        if (i > 0) {
            System.out.println("成功");
        }

        JDBCUtils.closeResource(connection, preparedStatement, null);
    }
}
